import { dateFormat } from '../../utils/dateFormat';

Component({
    properties: {
        placeholder: {
            type: String,
            value: '搜索你喜欢的商品'
        }
    },
    data: {
        value: '',
        isShowBtn: false,
        entrenceTime: dateFormat('yyyy-MM-dd hh:mm:ss:S')
    },
    methods: {
        searchBrands(e) {
            const { value } = e.detail;
            const self = this;
            let arr = [], history = wx.getStorageSync('searchHistory') || [];
            const index = history.findIndex(item => item == value);
            const opt_time = dateFormat('yyyy-MM-dd hh:mm:ss:S');

            if (index >= 0) {
                history = history.splice(index, 1);
            }

            arr = ([].concat([value], history)).slice(0, 9);
            wx.setStorageSync("searchHistory", arr);

            wx.navigateTo({
                url: '/pages/categoryHall/categoryHall?keywords=' + value + '&cur_page_resource_id='+ value +'&isSearch=6&ref_page_id=OFSearchViewController&ref_opt_resource_type=5&ref_opt_protocol=/pages/search/searchResult/searchResult&opt_resource_id=' + value + '&opt_protocol=/pages/categoryHall/categoryHall&opt_time=' + opt_time + '&log_type=1&ref_write_sever_timestamp=' + this.data.entrenceTime,
                complete() {
                    self.setData({
                        value: ''
                    })
                }
            });
        },
        foucsHandler() {
            this.setData({
                isShowBtn: true
            })
        },
        blurHandler() {
            this.setData({
                isShowBtn: false
            })
        },
        cancelHanlder() {
            wx.navigateBack()
        }
    }
})
